//
//  HomeTransferOrderViewController.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/3.
//

#import "HomeTransferOrderViewController.h"
#import "HomeTransferOrderInfoViewController.h"

#import "HomeTransferOrderTableViewCell.h"
#import "HomeTransforOrderNetWorking.h"

#import "HomeTransforOrderModel.h"

#import "publicNodataView.h"


@interface HomeTransferOrderViewController ()<UITableViewDelegate,UITableViewDataSource>{
//    NSInteger pageNo;
//    NSInteger pageSize;
}

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;

@property (nonatomic,strong) HomeTransforOrderNetWorking * manager;
@property (nonatomic,strong) NSMutableDictionary * paramer;
@property (nonatomic,strong) publicNodataView * nodataView;



@end

@implementation HomeTransferOrderViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadNet];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"转单申请";
    self.view.backgroundColor = COLOR_RGB(241, 241, 241);
    
//    pageNo = 1;
//    pageSize = 20;
    
    
    [self setUpUI];
//    [self loadNet];
    
}

-(void)setUpUI{
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.nodataView];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(StatusBarHeight);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    [self.nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(StatusBarHeight);
        make.left.right.bottom.equalTo(self.view);
    }];
    
}


-(void)loadNet{
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"加载中...";
    
    [self.manager dispatchTaskWithParam:@{} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        [hud hideAnimated:YES];
        
        if (responseObject && [responseObject[@"code"] intValue] == 200){
            [self.dataSource removeAllObjects];
            [self.dataSource  addObjectsFromArray:[HomeTransforOrderModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]]];
               
            if (self.dataSource.count == 0) {
                self.nodataView.hidden = NO;
            }else{
                self.nodataView.hidden = YES;
            }
            
            
            [self.tableView reloadData];
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:nil];
            return;
        }
        
//        [self.tableView.mj_header endRefreshing];
        
    }];
    
}



#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTransferOrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTransferOrderTableViewCell"];
    if (!cell){
        cell = [[HomeTransferOrderTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeTransferOrderTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    HomeTransforOrderModel * model = self.dataSource[indexPath.row];
    cell.model = model;
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTransferOrderInfoViewController * vc = [HomeTransferOrderInfoViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(170);
}



#pragma mark -- 懒加载
-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            pageNo = 1;
//            [self.paramer addEntriesFromDictionary:@{@"pageNo":@(pageNo)}];
//            [self loadNet];
//        }];
//        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//            pageNo += 1;
//            [self.paramer addEntriesFromDictionary:@{@"pageNo":@(pageNo)}];
//            [self loadNet];
//        }];
    }
    return _tableView;
}


-(NSMutableArray *)dataSource{
    if (!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


-(HomeTransforOrderNetWorking *)manager{
    if (!_manager){
        _manager = [HomeTransforOrderNetWorking new];
    }
    return _manager;
}

-(NSMutableDictionary *)paramer {
    if (!_paramer){
        _paramer = [NSMutableDictionary dictionary];
    }
    return _paramer;
}

-(publicNodataView *)nodataView{
    if (!_nodataView) {
        _nodataView = [publicNodataView new];
        _nodataView.hidden = YES;
    }
    return _nodataView;
}

@end
